<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/2
 * Time: 下午2:57
 */

namespace app\controllers;


use app\librarys\BaseController;
use app\models\Merchant;
use phpDocumentor\Reflection\Types\Nullable;
use yii\data\Pagination;

class MerchantController extends BaseController
{
    public function actionMerchantList()
    {
        $count = Merchant::getCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Merchant::getList($pagination->page, $pagination->pageSize);
        return $this->render('merchant-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionMerchantDetailPartial()
    {
        $id = \Yii::$app->request->post('id');

        $item = Merchant::getDetailById($id);

        return $this->renderPartial('merchant-detail-partial', ['item' => $item]);
    }

    public function actionMerchantAuthPartial()
    {
        $id = \Yii::$app->request->post('id');

        $item = Merchant::getDetailById($id);

        $authRecordItems = Merchant::getAuthRecordAllList($id);

        $currentAuthRecord = null;
        if (count($authRecordItems) > 0) {
            if ($authRecordItems[0]['status'] == 0) { // 待审核
                $currentAuthRecord = array_shift($authRecordItems);
            }
        }

        return $this->renderPartial('merchant-auth-partial',
            [
                'item' => $item,
                'currentAuthRecord' => $currentAuthRecord,
                'authRecordItems' => $authRecordItems
            ]);
    }

    public function actionMerchantAuthSuccessHandler()
    {
        $authId = \Yii::$app->request->post('auth_id');
        $authRemark = \Yii::$app->request->post('auth_remark');
        Merchant::authSuccess($authId, $authRemark);
        return $this->renderJson();
    }

    public function actionMerchantAuthFailedHandler()
    {
        $authId = \Yii::$app->request->post('auth_id');
        $authRemark = \Yii::$app->request->post('auth_remark');
        Merchant::authFailed($authId, $authRemark);
        return $this->renderJson();
    }

    public function actionMerchantSetEnabledHandler()
    {
        $id = \Yii::$app->request->post('id');
        $isEnabled = \Yii::$app->request->post('is_enabled');
        Merchant::setEnabled($id, $isEnabled);
        return $this->renderJson();
    }
}